﻿using Loogn.OrmLite;
using MicroConfig.NetCore.Server.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MicroConfig.NetCore.Server.Services
{
    public class ProjectService
    {
        public static readonly ProjectService Instance = new ProjectService();

        private List<Project> s_cacheList = null;
        private bool reload = false;
        private object obj = new object();

        public List<Project> CacheList
        {
            get
            {
                lock (obj)
                {
                    if (reload || s_cacheList == null)
                    {
                        s_cacheList = SelectAll("");
                        reload = false;
                    }
                    return s_cacheList;
                }
            }
        }

        public List<Project> SelectAll(string kw)
        {
            kw = SqlInjection.Filter(kw);
            using (var db = DB.Open())
            {
                if (string.IsNullOrEmpty(kw))
                {
                    return db.Select<Project>();
                }
                else
                {
                    return db.SelectWhere<Project>(DictBuilder.LIKE("name", "%" + kw + "%"));
                }
            }
        }

        public ResultObject Edit(Project m)
        {
            if (string.IsNullOrEmpty(m.Name))
            {
                return new ResultObject
                {
                    Msg = "项目名称不能为空"
                };
            }

            var oldM = SingleByName(m.Name);
            if (m.Id == 0)
            {
                if (oldM != null)
                {
                    return new ResultObject
                    {
                        Msg = "项目名称已存在"
                    };
                }
                //添加
                m.AddTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                using (var db = DB.Open())
                {
                    db.Insert(m);
                }
            }
            else
            {
                if (oldM != null && oldM.Id != m.Id)
                {
                    return new ResultObject
                    {
                        Msg = "项目名称已存在"
                    };
                }
                //修改
                using (var db = DB.Open())
                {
                    db.Update<Project>(m, "name", "token");
                }
            }
            reload = true;
            return new ResultObject
            {
                Success = true,
                Msg = "操作成功"
            };
        }


        public Project SingleByName(string name)
        {
            using (var db = DB.Open())
            {
                return db.SingleWhere<Project>(DictBuilder.Assign("Name", name));
            }
        }

        public ResultObject Delete(long id)
        {
            using (var db = DB.Open())
            {

                var count = db.CountWhere<ConfigItem>("ProjectId", id);
                if (count > 0)
                {
                    return new ResultObject { Msg = "项目下有配置项，不能删除" };
                }

                var flag = db.DeleteById<Project>(id);
                reload = true;
                return new ResultObject { Msg = "删除成功", Success = true };
            }
        }
    }
}
